<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Name:  Post Model
*
* Author:  William Bermudo
*
*/

class Post_m extends DataMapperExt
{
	public $table = 'posts';
	
	public $has_one = array(
						'user' => array(
							'class'			=> 'users_m',
							'other_field' 	=> 'posts',
							'join_other_as'	=> 'author',
							'join_table' 	=> 'posts'
						),
						'category' => array(
							'class'			=> 'blog_categories_m',
							'other_field'	=> 'posts',
							'join_other_as'	=> 'category',
							'join_table'	=> 'posts'
						)
					);
	
	public function __construct($id = NULL)
	{
		parent::__construct($id);
	}
	
	public function get_post_by_url($year, $month, $day, $url_title)
	{
		$date = $year . '-' . $month . '-' . $day;
		
		$posts = new Post_m();
		
		$posts
			->where('status', 'live')
			->where('date_posted', $date)
			->where('url_title', $url_title)
			->where('is_active', '1')
			->include_related('user', array('id', 'username'), TRUE, TRUE)
			->include_related('category', array('id', 'title'), TRUE, TRUE)
			->limit(1)
			->get();
				
		if ($posts->result_count() == 1)
		{
			return $posts;
		}
	}
}